package com.foxzzz.temporary;

// ?
public class Solution240 {
    private static int[][] genTestCast(String s, int w) {
        String[] array = s.replace("[", "").replace("]", "").split(",");
        int[][] result = new int[array.length / w][w];
        for (int i = 0; i < array.length; i++) {
            int x = i % w;
            int y = i / w;
            result[y][x] = Integer.parseInt(array[i]);
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] cast = genTestCast("[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]", 5);
        int[][] cast1 = genTestCast("[[1,4],[2,5]]", 2);
        System.out.println(new Solution240().searchMatrix(cast, 6));
    }

    public boolean searchMatrix(int[][] matrix, int target) {
        boolean result = false;
        if (matrix.length > 0 && matrix[0].length > 0) {
            int low = 0;
            int maxHigh = matrix.length < matrix[0].length ? matrix[0].length : matrix.length;
            int high = maxHigh;
            int rx = 0, ry = 0;
            int x = 0, y = 0;
            while (low <= high) {
                int mid = (low + high) >> 1;
                x = matrix[0].length - mid;
                y = matrix.length - mid;
                if (x < 0 || y < 0 || x >= matrix[0].length || y >= matrix.length)
                    break;
                if (matrix[y][x] == target) {
                    result = true;
                    break;
                }
                if (target > matrix[y][x]) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }

            System.out.println(x + "," + y);
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    if(i<=y&&j<=x)
                        continue;

                }
            }
        }
        return result;
    }
}
